package list

import "gitee.com/kristas/booting-go/framework/common/util/ccmap"

type ConcurrentSets struct {
	cm *ccmap.ConcurrentMap
}

func NewConcurrentSets(arr ...string) (s *ConcurrentSets) {
	s = &ConcurrentSets{
		cm: ccmap.NewConcurrentMap(),
	}
	s.PutAll(arr...)
	return
}

func (r *ConcurrentSets) Put(s string) {
	r.cm.Set(s, struct{}{})
}

func (r *ConcurrentSets) PutAll(arr ...string) {
	for _, s := range arr {
		r.Put(s)
	}
}

func (r *ConcurrentSets) ToArray() (arr []string) {
	r.cm.ForEach(func(k string, v interface{}) {
		arr = append(arr, k)
	})
	return
}

func (r *ConcurrentSets) Exists(t string) (ok bool) {
	return r.cm.Exists(t)
}

func (r *ConcurrentSets) ExistsAny(t ...string) (ok bool) {
	for _, s := range t {
		if r.Exists(s) {
			return true
		}
	}
	return false
}

func (r *ConcurrentSets) ExistsAll(t ...string) (ok bool) {
	for _, s := range t {
		if !r.Exists(s) {
			return false
		}
	}
	return true
}

func (r *ConcurrentSets) Remove(s string) {
	r.cm.Delete(s)
}

func (r *ConcurrentSets) RemoveAll(arr ...string) {
	for _, s := range arr {
		r.Remove(s)
	}
}

func (r *ConcurrentSets) Length() int {
	return r.cm.Size()
}

func (r *ConcurrentSets) ForEach(accept func(key string)) {
	r.cm.ForEach(func(k string, v interface{}) {
		accept(k)
	})
}
